// 题意：给定a, b, c, d, k，求a<=x<=b, c<=y<=d的gcd(x, y)=k的对数
//
// 题解：容斥原理处理范围，然后转为莫比乌斯反演，分块搞。
//
// 统计：10780ms, 19min, 1a
//
// run: $exec < input
#include <cstdio>
#include <algorithm>

int const maxn = 50010;
int mu[maxn], sum[maxn], prime[maxn];
bool not_prime[maxn];
int k, tot;

void init_mobius(int n = maxn)
{
	mu[1] = 1;
	for (int i = 2; i < n; i++) {
		if (!not_prime[i]) prime[++tot] = i, mu[i] = -1;
		for (int j = 1; prime[j] * i < n; j++) {
			not_prime[i * prime[j]] = true;
			if (i % prime[j] == 0) {
				mu[i * prime[j]] = 0;
				break;
			}
			mu[i * prime[j]] = -mu[i];
		}
	}
	for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + mu[i];
}

long long inverse(int n, int m)
{
	if (n > m) std::swap(n, m);
	long long ans = 0, pos = 1;
	for (int i = pos; i <= n; i = pos + 1) {
		pos = std::min(n / (n/i), m / (m/i));
		ans += (long long)(sum[pos] - sum[i - 1]) * (n/i) * (m/i);
	}
	return ans;
}

long long calc(int n, int m)
{
	n /= k; m /= k;
	return inverse(n, m);
}

int main()
{
	init_mobius();
	int T; std::scanf("%d", &T);
	while (T--) {
		int a, b, c, d;
		std::scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
		std::printf("%lld\n", calc(b, d) - calc(a-1, d) - calc(b, c-1) + calc(a-1, c-1));
	}
}

